const mongoose = require('mongoose')

const reviewSchema = new mongoose.Schema(
  {
    content: {
      type: String,
      required: [true, '评论内容不能为空'],
      trim: true,
    },
    // 评论作者
    user: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',
      required: true,
    },
    // 评论属于哪篇文章
    article: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Article',
      required: true,
    },
    // 如果是回复，则有父评论 ID
    parentReview: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Review',
      default: null, // 顶级评论为 null
    },
    likes: {
      type: Number,
      default: 0,
    },
    state: {
      type: String,
      enum: ['pending', 'approved', 'rejected', 'deleted'],
      default: 'pending', // 待审核, 已通过, 已拒绝,用户删除或管理员删除(逻辑删除)
    },
  },
  { timestamps: true },
)

module.exports = mongoose.model('Review', reviewSchema)
